Rails - undefined method `name' for nil:NilClass

Posted by sscirrus on Stack Overflow See other posts from Stack Overflow or by sscirrus
Published on 2010-05-16T19:51:59Z Indexed on 2010/05/16 20:20 UTC
Read the original article Hit count: 220

Hi guys,

Quick question. Here is my code:

#routes
map.resources :customers, :has_many => [:addresses, :matchings]
map.connect ":controller/:action/:id"

#url path:
http://127.0.0.1:3000/customers/index/3

#customers controller
def index
  @customer = Customer.find(params[:id])
end

#customers view/index.html.erb
...
<%= @customer.name %>
...

Error: undefined method `name' for nil:NilClass.

Here's my reasoning. The parameter :id is coming from my url path (i.e. we're looking for customer #3 in the above path). @customer should find that array easily, then @customer.name should produce the name, but apparently @customer is blank. Why?

I assume the problem is that I'm not producing an array in my controller?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about controller